/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
    // 判断是否为空
    if (head == null) {
        return null
    }

    // 判断头部
    while (head.val == val) {
        head = head.next;
        if (head == null) {
            return null
        }
    }

    let cur = head;
    while (cur) {
        if (cur.next?.val == val) {
            cur.next = cur.next.next;
        } else {
            cur = cur.next;
        }
    }

    return head;
};